{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "import pandas as pd\n",
    "\n",
    "import numpy as np\n",
    "import scipy.stats as stats\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "DATA_PATH = r'E:\\Codes\\UndergraduateFinalDesign\\e-based-qt\\data\\sh000300withemotion.csv'\n",
    "data = pd.read_csv(DATA_PATH, index_col='date', parse_dates=True)\n",
    "columns = [f'emotion_{fid + 1}' for fid in range(15)]\n",
    "\n",
    "factor = pd.DataFrame(index=data.index, columns=columns, data=data[columns])\n",
    "price = data['close']"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [],
   "source": [
    "\n",
    "def calculate_multi_factor_ic(factor_df, price_series, forecast_length):\n",
    "    \"\"\"\n",
    "    计算多因子IC（Information Coefficient）\n",
    "\n",
    "    参数:\n",
    "    factor_df (pandas.DataFrame): 包含因子数据的DataFrame，每列代表一个因子，索引为日期\n",
    "    price_series (pandas.Series): 价格序列，索引为日期，值为价格\n",
    "    forecast_length (int): 预测长度，代表未来收益率的计算窗口大小\n",
    "\n",
    "    返回:\n",
    "    float: 多因子IC值\n",
    "    \"\"\"\n",
    "    # 将价格序列向前平移forecast_length天，以获得未来收益率\n",
    "    future_returns = price_series.pct_change(forecast_length).shift(-forecast_length)\n",
    "\n",
    "    # 剔除包含NaN值的行\n",
    "    valid_rows = ~future_returns.isna()\n",
    "    factor_df = factor_df[valid_rows]\n",
    "    future_returns = future_returns[valid_rows]\n",
    "\n",
    "    # 计算每个因子与未来收益率的相关系数\n",
    "    _ic_values = []\n",
    "    _p_values = []\n",
    "    for i, [factor_name, factor_values] in enumerate(factor_df.items()):\n",
    "        ic, p = stats.spearmanr(factor_values, future_returns)\n",
    "        print(f\"for emotion_{i}\\t ic:\\t {ic:.5f},\\t\\t p:\\t {p}\")\n",
    "        _ic_values.append(ic)\n",
    "        _p_values.append(p)\n",
    "\n",
    "    # 计算多因子IC的平均值\n",
    "    multi_factor_ic = np.max(np.abs(_ic_values))\n",
    "\n",
    "    return multi_factor_ic, _ic_values, _p_values\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "for emotion_0\t ic:\t -0.02890,\t\t p:\t 0.19350021893979366\n",
      "for emotion_1\t ic:\t 0.05360,\t\t p:\t 0.015829909865590783\n",
      "for emotion_2\t ic:\t -0.01037,\t\t p:\t 0.6410090173073096\n",
      "for emotion_3\t ic:\t 0.03464,\t\t p:\t 0.11910900086878283\n",
      "for emotion_4\t ic:\t -0.02283,\t\t p:\t 0.30429373274725674\n",
      "for emotion_5\t ic:\t -0.01893,\t\t p:\t 0.3945112579997915\n",
      "for emotion_6\t ic:\t -0.07136,\t\t p:\t 0.0013089452113681493\n",
      "for emotion_7\t ic:\t -0.00057,\t\t p:\t 0.979381195832338\n",
      "for emotion_8\t ic:\t -0.01184,\t\t p:\t 0.594328386356034\n",
      "for emotion_9\t ic:\t -0.02849,\t\t p:\t 0.19988597292539123\n",
      "for emotion_10\t ic:\t -0.04080,\t\t p:\t 0.06635199526183232\n",
      "for emotion_11\t ic:\t -0.00186,\t\t p:\t 0.9333497736096245\n",
      "for emotion_12\t ic:\t -0.01644,\t\t p:\t 0.45948864449563576\n",
      "for emotion_13\t ic:\t -0.02208,\t\t p:\t 0.3204802152599496\n",
      "for emotion_14\t ic:\t 0.02752,\t\t p:\t 0.21558160200353674\n",
      "forecast 2 IC: 0.07135784708345853\n",
      "for emotion_0\t ic:\t -0.04911,\t\t p:\t 0.02717829326701455\n",
      "for emotion_1\t ic:\t 0.06383,\t\t p:\t 0.004076807130027186\n",
      "for emotion_2\t ic:\t -0.00793,\t\t p:\t 0.7215403958083781\n",
      "for emotion_3\t ic:\t 0.02767,\t\t p:\t 0.2135729779583802\n",
      "for emotion_4\t ic:\t -0.02167,\t\t p:\t 0.3300656095886755\n",
      "for emotion_5\t ic:\t -0.03771,\t\t p:\t 0.08998134052897479\n",
      "for emotion_6\t ic:\t -0.08509,\t\t p:\t 0.0001273546450696411\n",
      "for emotion_7\t ic:\t 0.00095,\t\t p:\t 0.9658949909386907\n",
      "for emotion_8\t ic:\t 0.00995,\t\t p:\t 0.6546142408689841\n",
      "for emotion_9\t ic:\t -0.02216,\t\t p:\t 0.3192210813156218\n",
      "for emotion_10\t ic:\t -0.05430,\t\t p:\t 0.014588340699910092\n",
      "for emotion_11\t ic:\t -0.02047,\t\t p:\t 0.3575154237654071\n",
      "for emotion_12\t ic:\t -0.03047,\t\t p:\t 0.17067972310157878\n",
      "for emotion_13\t ic:\t -0.02212,\t\t p:\t 0.3200218243857762\n",
      "for emotion_14\t ic:\t 0.02776,\t\t p:\t 0.2119868179162528\n",
      "forecast 5 IC: 0.08508501849309756\n",
      "for emotion_0\t ic:\t -0.02631,\t\t p:\t 0.2374303754773633\n",
      "for emotion_1\t ic:\t 0.01408,\t\t p:\t 0.5273066727668168\n",
      "for emotion_2\t ic:\t 0.00548,\t\t p:\t 0.8054934897999159\n",
      "for emotion_3\t ic:\t 0.00868,\t\t p:\t 0.6967996518063302\n",
      "for emotion_4\t ic:\t -0.04953,\t\t p:\t 0.026087050133723685\n",
      "for emotion_5\t ic:\t -0.06606,\t\t p:\t 0.002988159392248525\n",
      "for emotion_6\t ic:\t -0.09866,\t\t p:\t 8.992961500054755e-06\n",
      "for emotion_7\t ic:\t 0.01457,\t\t p:\t 0.5130083879742275\n",
      "for emotion_8\t ic:\t 0.03615,\t\t p:\t 0.10450538266009171\n",
      "for emotion_9\t ic:\t -0.04732,\t\t p:\t 0.033532116710210676\n",
      "for emotion_10\t ic:\t -0.04347,\t\t p:\t 0.05089944273915158\n",
      "for emotion_11\t ic:\t -0.02216,\t\t p:\t 0.31967715878331027\n",
      "for emotion_12\t ic:\t 0.00238,\t\t p:\t 0.9150353130009332\n",
      "for emotion_13\t ic:\t -0.00323,\t\t p:\t 0.8845518335243575\n",
      "for emotion_14\t ic:\t 0.07621,\t\t p:\t 0.0006114727389436476\n",
      "forecast 10 IC: 0.09865912078667283\n",
      "for emotion_0\t ic:\t -0.05554,\t\t p:\t 0.01279738036428464\n",
      "for emotion_1\t ic:\t -0.03648,\t\t p:\t 0.10219533328368284\n",
      "for emotion_2\t ic:\t 0.03941,\t\t p:\t 0.07747794495242467\n",
      "for emotion_3\t ic:\t 0.01429,\t\t p:\t 0.5223279845477451\n",
      "for emotion_4\t ic:\t -0.07466,\t\t p:\t 0.0008134257619081863\n",
      "for emotion_5\t ic:\t -0.07083,\t\t p:\t 0.0014943152944348645\n",
      "for emotion_6\t ic:\t -0.08250,\t\t p:\t 0.00021478895599561976\n",
      "for emotion_7\t ic:\t 0.02494,\t\t p:\t 0.26391046014938785\n",
      "for emotion_8\t ic:\t 0.02378,\t\t p:\t 0.2867850063698618\n",
      "for emotion_9\t ic:\t -0.08032,\t\t p:\t 0.00031470218225141815\n",
      "for emotion_10\t ic:\t -0.10577,\t\t p:\t 2.033599466264365e-06\n",
      "for emotion_11\t ic:\t -0.04104,\t\t p:\t 0.06599222096500378\n",
      "for emotion_12\t ic:\t 0.01625,\t\t p:\t 0.466813929026233\n",
      "for emotion_13\t ic:\t 0.03826,\t\t p:\t 0.0865416681207682\n",
      "for emotion_14\t ic:\t 0.10761,\t\t p:\t 1.3444353432763991e-06\n",
      "forecast 20 IC: 0.10760893418382399\n",
      "for emotion_0\t ic:\t -0.15223,\t\t p:\t 8.910574555156883e-12\n",
      "for emotion_1\t ic:\t -0.02628,\t\t p:\t 0.241481930577974\n",
      "for emotion_2\t ic:\t 0.06836,\t\t p:\t 0.002290624951523268\n",
      "for emotion_3\t ic:\t 0.06015,\t\t p:\t 0.007307410683379744\n",
      "for emotion_4\t ic:\t -0.11925,\t\t p:\t 9.668607822206316e-08\n",
      "for emotion_5\t ic:\t -0.09581,\t\t p:\t 1.8762827727192036e-05\n",
      "for emotion_6\t ic:\t -0.10877,\t\t p:\t 1.1674013881167972e-06\n",
      "for emotion_7\t ic:\t -0.03062,\t\t p:\t 0.172332082438672\n",
      "for emotion_8\t ic:\t -0.03246,\t\t p:\t 0.14791220186168177\n",
      "for emotion_9\t ic:\t -0.09504,\t\t p:\t 2.1912566758916538e-05\n",
      "for emotion_10\t ic:\t -0.11978,\t\t p:\t 8.492252522153019e-08\n",
      "for emotion_11\t ic:\t -0.02374,\t\t p:\t 0.2901217246497955\n",
      "for emotion_12\t ic:\t 0.01536,\t\t p:\t 0.4936781719651506\n",
      "for emotion_13\t ic:\t 0.03203,\t\t p:\t 0.15340971859203784\n",
      "for emotion_14\t ic:\t 0.16627,\t\t p:\t 8.599507490192166e-14\n",
      "forecast 40 IC: 0.16626999600086054\n"
     ]
    }
   ],
   "source": [
    "# 调用函数计算多因子IC\n",
    "forecast = [2,5, 10, 20, 40]\n",
    "for _ in forecast:\n",
    "    max_ic, ic_values, p_values = calculate_multi_factor_ic(factor, price, forecast_length=_)\n",
    "    print(f\"forecast {_} IC: {max_ic}\")\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
